package com.example.myviewpagerandfragment.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myviewpagerandfragment.R;

import java.util.ArrayList;
import java.util.List;

public class MyViewPagerAdapter extends RecyclerView.Adapter<MyViewPagerAdapter.MyViewPagerViewHolder> {

    private List<String> titles = new ArrayList<>(10);

    public MyViewPagerAdapter() {
        for (int i = 0; i < 10; i++) {
            titles.add("山东高速股份有限公司：" + i);
        }
    }

    @NonNull
    @Override
    public MyViewPagerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 解析布局文件
        return new MyViewPagerViewHolder(LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_pager, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewPagerViewHolder holder, int position) {
        holder.mTv.setText(titles.get(position));
    }

    @Override
    public int getItemCount() {
        return titles == null ? 0 : titles.size();
    }

    static class MyViewPagerViewHolder extends RecyclerView.ViewHolder {

        TextView mTv;

        RelativeLayout mContainer;

        public MyViewPagerViewHolder(@NonNull View itemView) {
            super(itemView);
            mContainer = itemView.findViewById(R.id.rl_container);
            mTv = itemView.findViewById(R.id.tv_title);
        }
    }
}
